// pages/shoppingMall/orderDetail.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "订单详情", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    goodsArr: [],
    orderId: 0,
    parentId: 0,
    shareTitle: '',
    shareImg: '',
    openid: '',
    orderInfo: [],
    shopOrderStatusArr: [],
    isOne: 1,
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  tel() {
    wx.makePhoneCall({
      phoneNumber: '023-81395565',
    })
  },
  copy() {
    var that = this;
    wx.setClipboardData({
      data: that.data.orderInfo.wl_order_number, // 要复制的文本内容
      success(res) {
        app.showTip("复制成功")
      },
      fail(err) {
        app.showTip("复制失败")
      }
    });
  },
  //商品评价
  evaluate() {
    var id = this.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/shoppingMall/evaluate?smallOrderId=' + id + "&isEdit=0"
    })
  },
  //查看评价
  lookEvaluate() {
    var id = this.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/shoppingMall/selfEvaluate?smallOrderId=' + id
    })
  },
  //删除商城订单
  delShopOrder() {
    var that = this;
    var status = that.data.orderInfo.status;
    var id = that.data.orderInfo.id;
    var parentId = that.data.orderInfo.parentId;

    //提示用户是否确认取消订单
    wx.showModal({
      title: '温馨提示',
      content: '您确认删除此订单吗?',
      complete: (res) => {
        if (res.confirm) {
          //确认删除
          wx.showLoading({
            title: '订单删除中...',
            mask: true
          })
          wx.request({
            url: that.data.baseUrl + 'delShopOrder.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'id': id,
              'parentId': parentId,
              'orderStatus': status
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              app.showTip(res.data.msg);
              if (res.data.status == 1) {
                //重新加载数据
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1000);
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
            }
          });
        }
      }
    });
  },

  //商城申请退款
  shopRefund() {
    var that = this;
    var status = that.data.orderInfo.status;
    var id = that.data.orderInfo.id;
    wx.navigateTo({
      url: '/pages/shoppingMall/refund?orderId=' + id
    });
  },

  //撤销退款
  resetRefund() {
    var that = this;
    var id = that.data.orderInfo.id;
    //提示用户是否确认撤销退款
    wx.showModal({
      title: '温馨提示',
      content: '请再次确认是否撤销退款',
      complete: (res) => {
        if (res.confirm) {
          //确认删除
          wx.showLoading({
            title: '撤销退款中...',
            mask: true
          })
          wx.request({
            url: that.data.baseUrl + 'cancelShopOrderRefund.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'orderId': id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              app.showTip(res.data.msg);
              if (res.data.status == 1) {
                //重新加载数据
                setTimeout(function () {
                  that.getShopOrderDetailPageInfo(that.data.openid, that.data.orderId, that.data.parentId);
                }, 1000);
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
            }
          });
        }
      }
    });
  },
  //前往支付
  goPay() {
    var that = this;
    var orderId = that.data.orderInfo.parentId;
    var shop_id = that.data.orderInfo.shop_id;
    wx.navigateTo({
      url: '/pages/shoppingMall/order?orderId=' + orderId + "&shop_id=" + shop_id
    });
  },
  //确认收货
  enterSh() {
    var that = this;
    var id = that.data.orderInfo.id;
    //提示用户是否确认收货
    wx.showModal({
      title: '温馨提示',
      content: '请再次确认是否收到商品',
      complete: (res) => {
        if (res.confirm) {
          //确认删除
          wx.showLoading({
            title: '确认收货中...',
            mask: true
          })
          wx.request({
            url: that.data.baseUrl + 'enterShopOrder.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'orderId': id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              app.showTip(res.data.msg);
              if (res.data.status == 1) {
                //重新加载数据
                setTimeout(function () {
                  that.getShopOrderDetailPageInfo(that.data.openid, that.data.orderId, that.data.parentId);
                }, 1000);
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
            }
          });
        }
      }
    });
  },
  //重新购买
  copyShopping() {
    var that = this;
    var status = that.data.orderInfo.status;
    var id = that.data.orderInfo.id;
    var parentId = that.data.orderInfo.parentId;
    //提示用户是否重新购买
    wx.showModal({
      title: '温馨提示',
      content: '请再次确认是否重新购买此订单中的商品',
      complete: (res) => {
        if (res.confirm) {
          //确认删除
          wx.showLoading({
            title: '商品购买中...',
            mask: true
          })
          wx.request({
            url: that.data.baseUrl + 'copyShoppingOrder.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'parentId': parentId
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              if (res.data.status == 1) {
                //重新加载数据
                wx.navigateTo({
                  url: '/pages/shoppingMall/order?orderId=' + res.data.orderId + "&shop_id=" + res.data.shop_id
                });
              } else {
                app.showTip(res.data.msg);
              }
            },
            fail: function () {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
            }
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var orderId = options.orderId;
    var parentId = options.parentId;
    if ((orderId == '' || orderId == null || orderId == undefined) || (parentId == '' || parentId == null || parentId == undefined)) {
      app.showTip('未获取到订单ID哦');
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 1300)
      return;
    } else {
      that.setData({
        parentId: parentId,
        orderId: orderId
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');
    openid=''
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getShopOrderDetailPageInfo(res.data.openid, orderId, parentId);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getShopOrderDetailPageInfo(openid, orderId, parentId);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getShopOrderDetailPageInfo(openid, orderId, parentId) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getShopOrderDetailPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'orderId': orderId,
        'parentId': parentId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            orderInfo: res.data.data,
            shopOrderStatusArr: res.data.shopOrderStatusArr
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    if (that.data.isOne == 1) {
      that.setData({
        isOne: 0
      });
    } else {
      //刷新页面数据
      that.getShopOrderDetailPageInfo(that.data.openid, that.data.orderId, that.data.parentId);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})